package com.example.module_word.room;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.sqlite.db.SupportSQLiteDatabase;
import com.example.module_word.room.entity.JiHua;
import com.example.module_word.room.entity.MyStack;
import com.example.module_word.room.entity.MyWord;
import com.fenghuajueli.libbasecoreui.BaseApplication;

@Database(entities = {Danci.class, MyStack.class, JiHua.class, MyWord.class},version = 1,exportSchema = false)
public abstract class DanCiDataBase extends RoomDatabase {
    private static final String DB_NAME = "word.db";
    private static volatile DanCiDataBase instance;
    public static synchronized DanCiDataBase getInstance() {
        if (instance == null) {
            instance = create();
        }
        return instance;
    }

    private static DanCiDataBase create() {
        return Room.databaseBuilder(
                BaseApplication.getApplication(),
                DanCiDataBase.class,
                DB_NAME)
                .createFromAsset("danci.db")//从assets复制
                .addCallback(new Callback() {
                    @Override
                    public void onCreate(@NonNull SupportSQLiteDatabase db) {
                        super.onCreate(db);
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                DanCiDataBase.create().getDanCiDao().insertMyStack(new MyStack("研究生",false));
                            }
                        }).start();
                    }
                })
                .allowMainThreadQueries()//允许在主线程允许
                .build();
    }


    public abstract DanCiDao getDanCiDao();
}
